<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of menuAction
 * 游戏管理
 * @author skyinter
 */
class gameAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    /**
     * 游戏列表
     */
    public function index() {

        $page = $this->_get('page') ? $this->_get('page') : '1';
        $where = '1=1';
        if ($_GET) {
            $keyword = $this->_get('keyword') ? $this->_get('keyword') : '';
            if($keyword) {
                $where .= " AND `name` like '%" . $keyword . "%'";
            }
            $letter  = $this->_get('letter' , '');
            if($letter) {
                $where .= " AND `letter`= '".$letter."'";
            }
        }
        $game_list = D('game')->get_list($page, $where);
        $total = D('game')->getTotal();
        $letters = array('A', 'B' , 'C' , 'D' , 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V' ,'W', 'X', 'Y', 'Z');
        $this->assign('list', $game_list);
        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('keyword', $keyword);
        $this->assign('letters' , $letters);
        $this->display();
    }



    /**
     * 添加商品
     */
    public function add() {
        //执行添加商品操作
        if ($_POST) {
            $tablefields = M('game')->getTableFields();
            $data = array();
            foreach ($tablefields['fields'] as $key => $value) {
                if (!isset($_POST[$key]))
                    continue;
                $data[$key] = trim($this->_post($key));
            }
            $result = D('game')->add($data);
            if ($result !== false) {
                $this->JsonReturn('添加成功', null, 1);
            } else {
                $this->JsonReturn('添加失败', null, 0);
            }
            exit;
        }
        $this->display();
    }

    /**
     * 商品编辑
     */
    public function edit() {

        if ($_POST) {
            $id = $this->_postid('id');
            $tablefields = M('game')->getTableFields();
            $data = array();
            foreach ($tablefields['fields'] as $key => $value) {
                if (!isset($_POST[$key]))
                    continue;
                $data[$key] = trim($this->_post($key));
            }
            $result = D('game')->modify($id, $data);
            if ($result !== false) {
                $this->JsonReturn('编辑成功', null, 1);
            } else {
                $this->JsonReturn('编辑失败', null, 0);
            }
            exit;
        }

        //游戏信息
        $id = $this->_get('id');
        $info = D('game')->info($id);
        $this->assign('info', $info);
        $this->display();
    }



    public function ajax() {
        $id = $this->_get('id') ? $this->_get('id') : null;
        $type = $this->_get('type') ? $this->_get('type') : null;

        switch ($type) {
            case 'down': //商品下架
                $data = array('state' => 2);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'del': //商品删除
                $stat = D('goods')->where(array('id'=>$id))->delete();
                break;
            case 'up': //商品上架
                $data = array('state' => 1);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'cateSave': //商品类别名称更改
                $data = array('name' => $this->_get('name'));
                $stat = D('goodsCategory')->modify($id, $data);
                break;
            case 'cateDel': //分类
                $result = D('goods')->where(array('cid'=>$id))->find();
                if($result) {
                    $this->JsonReturn('该分类下有产品，无法删除', null , 0);
                }
                $stat = D('goodsCategory')->where(array('id'=>$id))->delete();
                break;
            case 'cdel':
                $stat = D('order')->where(array('id' => $id))->delete();
                break;    
            default:
                $stat = 0;
                break;
        }

        if ($stat > 0) {
            $info = '请求成功';
            $this->JsonReturn($info, null, 1);
        } else {
            $info = '请求失败';
            $this->JsonReturn($info, null, 0);
        }
    }

}
